const express = require('express');
const { readFileSync } = require('fs');
const { resolve } = require('path');

const app = express();

//跨域
app.all('*', function (req, res, next) {
  res.header('Access-Control-Allow-Origin', '*');
  res.header('Access-Control-Allow-methods', 'POST,GET');

  next();
});

app.get('/v2/get_course_fields', function (req, res) {
  const courseFieldData = JSON.parse(
    readFileSync(resolve(__dirname, './data/courseField.json'), 'utf-8')
  );

  res.send(courseFieldData);
});

app.get('/v2/get_courses/all', function (req, res) {
  const courseListData = JSON.parse(
    readFileSync(resolve(__dirname, './data/courseList.json'), 'utf-8')
  );

  res.send(courseListData);
});

app.listen(8080, function () {
  console.log('welcome to use Express!');
});
